const DataMessage = {

    refreshOnline(topology, pen, value) {
        if (topology == null) {
            return;
        }
        // console.log(value)
        if (!value || value == 0) {
            pen.fontColor = "grey";
            pen.fillStyle = "grey";
        } else {
            pen.fontColor = "green";
            pen.fillStyle = "#00D73AFF";
        }
    },

    refreshPen(topology, pen, value) {
        if (topology == null) {
            return;
        }
        let v0 = {};
        let v1 = {};
        let hasChildren = false;

        if (pen.hasOwnProperty('children')) {
            let children = pen.children;
            if (children != null) {
                if (children.length > 0) {
                    hasChildren = true;
                    v0 = topology.find('v0', children, false);
                    v1 = topology.find('v1', children, false);
                }
            }
        }

        switch (pen.name) {
            case 'rectangle':// 光字牌
                {
                    let penData = pen.data;
                    if ((penData != null) && (!Array.isArray(penData))) {
                        if ((penData["ctrl_type"] == 5) || (!penData.hasOwnProperty("ctrl_type"))) {
                            console.log('if----', value)
                            if (value == 0) {
                                pen.fillStyle = '#00D73AFF';
                                pen.text = penData.v0;
                            } else {
                                pen.fillStyle = '#FC0808FF';
                                pen.text = penData.v1;
                            }
                        } else if (penData["ctrl_type"] == 6) {
                            let size = penData["enum"].length;
                            for (let i = 0; i < size; i++) {
                                if (value == penData["enum"][i]["value"]) {
                                    pen.fillStyle = penData["enum"][i]["fileStyle"];
                                    pen.text = penData["enum"][i]["text"];
                                    return;
                                }
                            }

                        }
                    } else {
                        // console.log('else----', value)
                        if (value == 0) {
                            pen.fillStyle = '#00D73AFF';
                        } else {
                            pen.fillStyle = '#FC0808FF';
                        }
                    }

                }
                // {
                //     let penData = pen.data;
                //     if (penData != null) {
                //         if ((penData["ctrl_type"] == 5) || (!penData.hasOwnProperty("ctrl_type"))) {
                //             if (value == 0) {
                //                 if (penData.length > 0 || Object.keys(penData).length > 0) {
                //                     pen.text = penData.v0;
                //                 }
                //                 pen.fillStyle = '#00D73AFF';


                //             } else {
                //                 if (penData.length > 0 || Object.keys(penData).length > 0) {
                //                     pen.text = penData.v1;
                //                 }
                //                 pen.fillStyle = '#FC0808FF';

                //             }
                //         } else if (penData["ctrl_type"] == 6) {
                //             let size = penData["enum"].length;
                //             for (let i = 0; i < size; i++) {
                //                 if (value == penData["enum"][i]["value"]) {
                //                     pen.fillStyle = penData["enum"][i]["fillStyle"];
                //                     pen.text = penData["enum"][i]["text"];
                //                     return;
                //                 }
                //             }

                //         }
                //     }

                // }
                break;
            case 'line':
            case 'curve':
                {
                    if (value == 0) {
                        pen.lineWidth = 2;
                        pen.strokeStyle = "gray";
                        pen.lineDash[0] = 2;
                        pen.lineDash[1] = 6;
                    } else {
                        pen.lineWidth = 2;
                        pen.strokeStyle = "#BD10E0";
                        pen.lineDash[0] = 0;
                        pen.lineDash[1] = 0;
                    }
                }
                break;
            case 'text':
                {
                    pen.text = value.toString();
                }
                break;
            case 'combine':
                {
                    if (hasChildren) {
                        if (value == 0) {
                            v0.visible = true;
                            v0.fontColor = "green";
                            v1.visible = false;
                        } else {
                            v0.visible = false;
                            v1.visible = true;
                            v1.fontColor = "red";
                        }

                    }
                }
                break;
            case 'icon':
                {
                    if (value == 0) {
                        pen.fontColor = "green";
                    } else {
                        pen.iconColor = 'red'
                        // pen.fillStyle = "red";
                    }
                }
            case 'image':
                {
                    if (value == 0) {
                        pen.fontColor = "#7ED321FF";
                        pen.iconColor = '#7ED321FF'
                    } else {
                        pen.fontColor = "#D0021BFF";
                        pen.iconColor = '#D0021BFF'
                    }
                }
                break;
            case 'circle':
                {
                    if (value == 0) {
                        pen.fillStyle = "#00D73AFF";
                    } else {
                        pen.fillStyle = "#F5A623FF";
                    }
                }
            default:
                break;
        }
    },

    onMsg(topology, msg) {
        if (topology == null) {
            return;
        }
        let type = msg.webMsgType;
        let funCode = msg.funCode;
        let data = msg.data;
        let tag = null;
        let value = 0;
        let pens = null;
        switch (type) {
            case 1007:
                {
                    // 网关消息
                    if (funCode == 6 || funCode == 0) {
                        tag = data.gw_sn;
                        value = data.online;
                        pens = topology.find(tag, true, true);
                        if (pens != null) {
                            let size = pens.length;
                            for (let i = 0; i < size; i++) {
                                this.refreshOnline(topology, pens[i], value);
                            }
                        }
                    }
                }
                break;
            case 1008:
                {
                    // 终端消息
                    if (funCode == 2 || funCode == 0) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let terminal = data[i];
                            let tag = terminal.id;
                            let value = terminal.online;
                            let pens = topology.find(tag, true, true)
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshOnline(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1009:
                {
                    // 遥信消息
                    if (funCode == 2 || funCode == 0) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let terminal = data[i];
                            let tag = terminal.id;
                            let value = terminal.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1010:
                {
                    // 遥测消息
                    if (funCode == 2 || funCode == 0) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let yc = data[i];
                            let tag = yc.id;
                            let value = yc.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1011:
                {
                    // 遥脉消息
                    if (funCode == 2 || funCode == 0) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let ym = data[i];
                            let tag = ym.id;
                            let value = ym.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1012:
                {
                    // 遥控消息
                    if (funCode == 3) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let yk = data[i];
                            let tag = yk.id;
                            let value = yk.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1013:
                {
                    // 遥调消息
                    if (funCode == 3) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let yt = data[i];
                            let tag = yt.id;
                            let value = yt.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    }
                }
                break;
            case 1015:
                {
                    // 定时任务消息
                    if (funCode == 0) {
                        let dataSize = data.length;
                        for (let i = 0; i < dataSize; i++) {
                            let task = data[i];
                            let tag = task.id;
                            let value = task.value;
                            let pens = topology.find(tag, true, true);
                            if (pens != null) {
                                let size = pens.length;
                                for (let j = 0; j < size; j++) {
                                    this.refreshPen(topology, pens[j], value);
                                }
                            }
                        }
                    } else if (funCode == 4) {
                        let tag = data.id;
                        let value = data.value;
                        let pens = topology.find(tag, true, true);
                        if (pens != null) {
                            let size = pens.length;
                            for (let j = 0; j < size; j++) {
                                this.refreshPen(topology, pens[j], value);
                            }
                        }
                    }
                }
                break;
            case 1019:
                {
                    // 告警消息
                    switch (funCode) {
                        case 0:
                            {
                                // 全部告警
                                let dataSize = data.length;
                                for (let i = 0; i < dataSize; i++) {
                                    this.onSingleAlarm(topology, data[i]);
                                }
                            }
                            break;
                        case 2:
                            {
                                // 单个告警
                                this.onSingleAlarm(topology, data);
                            }
                            break;
                        default:
                            break;
                    }
                }
                break;
        }

        topology.render();
    },

    onSingleAlarm(topology, alarm) {
        if (topology == null) {
            return;
        }
        let tag = alarm.id;
        let alarmType = alarm.alarm_type;
        let pens = topology.find(tag, true, true);

        if (pens != null) {
            let size = pens.length;
            for (let j = 0; j < size; j++) {
                let pen = pens[j];
                switch (alarmType) {
                    case 1:
                        {
                            // 遥信变位
                            let value = alarm.value;
                            this.refreshPen(pen, value);
                        }
                        break;
                    case 2:
                        {
                            // 遥测越限
                            let value = alarm.value;
                            pen.text = value.toString();
                            switch (alarm.alarmType) {
                                case 0:
                                    {
                                        pen.fontColor = "green";
                                        pen.textBackground = "";
                                    }
                                    break;
                                case 1:
                                case 4:
                                    {
                                        pen.fontColor = "red";
                                        // pen.textBackground = "red";
                                    }
                                    break;
                                case 2:
                                case 3:
                                    {
                                        pen.fontColor = "yellow";
                                        // pen.textBackground = "yellow";
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                    case 3:
                        {
                            // 网关离线
                            let online = alarm.online;
                            if (pen.type == 0) {
                                if (pen.image == null && pen.icon == null) {
                                    pen.text = online.toString();
                                }
                                if (online) {
                                    pen.fontColor = "green";
                                } else {
                                    pen.fontColor = "gray";
                                }
                            } else {
                                if (online) {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "#BD10E0";
                                    pen.lineDash[0] = 0;
                                    pen.lineDash[1] = 0;
                                } else {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "gray";
                                    pen.lineDash[0] = 2;
                                    pen.lineDash[1] = 6;
                                }
                            }
                        }
                        break;
                    case 4:
                        {
                            // 终端离线
                            let online = alarm.online;
                            if (pen.type == 0) {
                                if (pen.image == null && pen.icon == null) {
                                    pen.text = online.toString();
                                }
                                if (online) {
                                    pen.fontColor = "green";
                                } else {
                                    pen.fontColor = "gray";
                                }
                            } else {
                                if (online) {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "#BD10E0";
                                    pen.lineDash[0] = 0;
                                    pen.lineDash[1] = 0;
                                } else {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "gray";
                                    pen.lineDash[0] = 2;
                                    pen.lineDash[1] = 6;
                                }
                            }
                        }
                        break;
                    case 5:
                        {
                            // 遥控事件
                            let value = alarm.value;
                            if (pen.type == 0) {
                                if (pen.image == null && pen.icon == null) {
                                    pen.text = value.toString();
                                }
                                if (value == 0) {
                                    pen.fontColor = "green";
                                } else {
                                    pen.fontColor = "red";
                                }
                            } else {
                                if (online) {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "#BD10E0";
                                    pen.lineDash[0] = 0;
                                    pen.lineDash[1] = 0;
                                } else {
                                    pen.lineWidth = 2;
                                    pen.strokeStyle = "gray";
                                    pen.lineDash[0] = 2;
                                    pen.lineDash[1] = 6;
                                }
                            }
                        }
                        break;
                    case 6:
                        {
                            // 遥调事件
                            let value = alarm.value;
                            pen.text = value.toString();
                            pen.fontColor = "green";
                        }
                        break;
                }
            }
        }
    },
};

export { DataMessage };
